# -*- coding:utf-8 -*-
'''
@Author: lindes
'''
from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver
from .models import PraisePost, InformPost, CollectPost, CommentPost


@receiver(post_save, sender=PraisePost)
def create_pa(sender, instance=None, created=False, **kwargs):
    if created:
        article = instance.post
        article.praise_count += 1
        article.save()


@receiver(post_delete, sender=PraisePost)
def delete_pa(sender, instance=None, created=False, **kwargs):
    article = instance.post
    article.praise_count -= 1
    article.save()


@receiver(post_save, sender=InformPost)
def create_ia(sender, instance=None, created=False, **kwargs):
    if created:
        article = instance.post
        article.inform_count += 1
        article.save()


@receiver(post_delete, sender=InformPost)
def delete_ia(sender, instance=None, created=False, **kwargs):
    article = instance.post
    article.inform_count -= 1
    article.save()


@receiver(post_save, sender=CollectPost)
def create_ca(sender, instance=None, created=False, **kwargs):
    if created:
        article = instance.post
        article.collect_count += 1
        article.save()


@receiver(post_delete, sender=CollectPost)
def delete_ca(sender, instance=None, created=False, **kwargs):
    article = instance.post
    article.collect_count -= 1
    article.save()


@receiver(post_save, sender=CommentPost)
def create_cma(sender, instance=None, created=False, **kwargs):
    if created:
        article = instance.post
        article.comment_count += 1
        article.save()


@receiver(post_delete, sender=CommentPost)
def delete_cma(sender, instance=None, created=False, **kwargs):
    article = instance.post
    article.comment_count -= 1
    article.save()
